package nl.sogyo.objectvisualisatie.data;

import java.util.ArrayList;
import java.util.TreeMap;

/**
 * SourceFileLoader keeps track of all the source files 
 * of a class that is in the monitoring JVM. It also checks
 * if a class is already loaded or not
 * @author Ben
 *
 */
public class SourceFileLoader 
{ 
	private TreeMap<String,SourceCode> listings; // TreeMap in which the filename and sourcecode will be saved
 
	public SourceFileLoader() 
	{  
		listings = new TreeMap<String,SourceCode>();  
	} 
 
	/**
	 * Add a new .java file to the list 
	 * @param sourcepath = sourcepath where the file is located
	 * @param fnm		 = filename of the .java-file	
	 */
	public void add(String sourcepath, String fnm )
	{ 
		if (listings.containsKey(fnm)) { //already listed 		
			return; 
		} 
		listings.put(fnm, new SourceCode(sourcepath,fnm)); //added to listings	
	}  
 
	/**
	 * Get a specific line of the source code
	 * @param fnm	  = filename of the sourcefile
	 * @param lineNum = linenumber that will be retrieved
	 * @return
	 */
	public String showLine(String fnm, int lineNum)
	{
		SourceCode lines = listings.get(fnm); 
		if (lines == null) 
			return (fnm + "not listed"); 
		return lines.showLine(lineNum); 
	}
  
	/**
	 * Get all the source code of specific file
	 * @param fnm = filename that will be retrieved
	 * @return
	 */
	public ArrayList<String> showAll(String fnm)
	{ 
		SourceCode lines = listings.get(fnm); 
		if (lines == null) 
			return null; 
		return lines.showAll(); 
	}
	
	/**
	 * Get a list of all java-files and SourceCode objects
	 * @return
	 */
	public TreeMap<String, SourceCode> getTreeMap()
	{
		return listings;		
	}
	
	public void clearTreeMap()
	{
		listings.clear();		
	}
}